package com.briup.day20;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author ThinkBook
 */
public class Test01_HashMap {
    //定义一个Map集合对象，存储学生信息：Student-id对象
    public static void main(String[] args) {
        Map<Student,String> map = new HashMap<>();

        map.put(new Student("tom",21),"001");
        map.put(new Student("tom",21),"011");
        map.put(new Student("lucy",19),"005");
        map.put(new Student("jack",20),"007");
        map.put(new Student("rose",20),"021");
        // HashMap中 key、value都可以取值null
        map.put(null,"007");
        map.put(new Student("jack",20),null);
        //遍历集合
        for(Student s : map.keySet()) {
            System.out.println(s + ": " + map.get(s));
        }
    }
    public static void main01(String[] args) {
        Map<String,Student> map = new HashMap<>();
        map.put("001",new Student("tom",21));
        map.put("005",new Student("zs",20));
        map.put("003",new Student("lucy",19));
        map.put("004",new Student("jack",20));
        map.put("001",new Student("rose",20));

        if (map.containsKey("001")){
            Student s = map.get("001");
            System.out.println(s);
        }
        System.out.println("------------");
        Collection<Student> value = map.values();
        for (Student s : value){
            System.out.println(s);
        }
        System.out.println("-----------");
        Set<Map.Entry<String,Student>> entries = map.entrySet();
        for (Map.Entry<String,Student> entry: entries){
            String key = entry.getKey();
            Student values = entry.getValue();
            System.out.println(key+":"+values);
        }
    }
}
class Student {
    private String name;
    private int age;

    public Student() {
    }
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
